﻿/// <reference path="JQuery/jquery-1.6.2-vsdoc.js" />
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  Copyright(c)    2011    @kaleyroy   kaleyroy@gmail.com
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/***********************
//  文档加载
************************/
$(function () {
    /***********************
    //  初始化工作
    ************************/
    $("#level-window").window("close");

    //级别名称
    $("#levelname").validatebox({
        required: true,
        missingMessage: "级别名称为必填项!"
    });

    //单数
    $("#billcount").numberspinner({
        value: 1,
        min: 1,
        max: 100,
        required: true,
        missingMessage: "单数为必填项!"
    });

    //标识颜色
    $("#levelcolor").ColorPicker({
        onSubmit: function (hsb, hex, rgb, el) {
            $(el).val(hex);
            $(el).ColorPickerHide();
            $("#levelcolor").css("backgroundColor", "#" + hex);
        },
        onBeforeShow: function () {
            $(this).ColorPickerSetColor(this.value);
        }
    }).bind('keyup', function () {
        $(this).ColorPickerSetColor(this.value);
    });
    $("#levelcolor").validatebox({
        required: true,
        missingMessage: "标识颜色为必填项!"
    });

    /** 级别表单处理 **/
    $("#handle-form").click(function () {
        var url = "";
        var title = "";
        var success_msg = "";
        var error_msg = "";
        var invalid_msg = "";
        var act = $("#handle-form").attr("act");
        if (!act) { return; }
        if (act == "new") {
            url = "../Level/New";
            title = "新建级别";
            success_msg = "新级别信息创建成功";
            error_msg = "新级别信息创建异常,请重试";
            invalid_msg = "新级别信息无效,请重试";
        } else if (act == "edit") {
            url = "../Level/Edit";
            title = "编辑级别";
            success_msg = "级别信息编辑成功";
            error_msg = "级别信息编辑异常,请重试";
            invalid_msg = "级别信息无效,请重试";
        }
        $("#level-form").form("submit", {
            url: url,
            onSubmit: function () {
                return $("#level-form").form("validate");
            },
            success: function (data) {
                var o = jQuery.parseJSON(data);
                if (isSuccess(o)) {
                    gritter(title, success_msg, success);
                    $("#level-form").form("clear");
                    $("#level-window").window("close");
                    $("#level-table").datagrid("reload");
                } else if (isError(o)) {
                    gritter(title, error_msg, error);
                } else if (isInvalid(o)) {
                    gritter(title, invalid_msg, error);
                }
            }
        });
    });

    /** 重置级别表单 **/
    $("#reset-form").click(function () {
        //$("#level-form").form("clear");
        $("#level-form").form("load", "../Scripts/Jsons/level-form.json");
        $("#levelcolor").css("backgroundColor", "#f0f0f0");
    });

    /** 级别列表数据 **/
    $('#level-table').datagrid({
        //title: "级别管理",
        iconCls: 'icon-save',
        fitColumns: true,
        collapsible: true,
        url: "../Level/List",
        sortName: 'LevelID',
        sortOrder: 'asc',
        remoteSort: false,
        singleSelect: true,
        idField: 'LevelID',
        frozenColumns: [[
	                    { field: "ck", checkbox: true },
	                    { title: '级别编号', field: "LevelID", width: 80, align: "center", sortable: true }
				    ]],
        columns: [[
			            { title: "级别信息", colspan: 11 }
				    ], [
					    { field: "LevelName", title: "级别名称", width: 100, align: "center" },
					    { field: "BillCount", title: "单   数", width: 100, align: "center" },
					    { field: "LevelColor", title: "标识颜色", width: 100, align: "center",
					        formatter: function (value, rec) {
					            return "<div style='width:48px;height:16px;margin:auto;background:#" + value + "'> </div>";
					        }
					    },
					    { field: "IsEnabled", title: "状   态", width: 100, sortable: true, align: "center",
					        formatter: function (value, rec) {
					            return value ? "<div style='cursor:pointer;width:16px;height:16px;margin:auto;' class='icon-enabled'> </div>" : "<div style='cursor:pointer;width:16px;height:16px;margin:auto;' class='icon-invalid'> </div>";
					        }
					    }
				    ]],
        pagination: true,
        rownumbers: true,
        toolbar: "#leveltable-toolbar",
        rowStyler: function (index, row, css) {
            if (row.IsEnabled == 0) {
                return "color:#ddd;";
            }
        },
        onClickRow: function (rowIndex, rowData) {
            var act = rowData.IsEnabled ? "allow" : "forbid";
            eabledButtons(act);
        }
    });

    /** 新建级别 **/
    $("#new-level").click(function () {
        $("#level-window").window({
            title: "新建级别",
            iconCls: "icon-tableadd"
        }, "open");
        $("#level-form").form("load", "../Scripts/Jsons/level-form.json");
        $("#levelcolor").css("backgroundColor", "#f0f0f0");
        $("#handle-form").attr("act", "new");
        $("#handle-form").linkbutton({ text: "添加", iconCls: "icon-tablerowinsert" });
        $("#reset-form").linkbutton({ text: "重置" });
    });

    /** 编辑级别 **/
    $("#edit-level").click(function () {
        var selected = $("#level-table").datagrid("getSelected");
        if (selected) {
            $("#level-form").form("load", {
                "LevelID": selected.LevelID,
                "LevelName": selected.LevelName,
                "BillCount": selected.BillCount,
                "LevelColor": selected.LevelColor,
                "IsEnabled": "" + selected.IsEnabled
            });
            $("#levelcolor").css("backgroundColor", "#" + selected.LevelColor);
            $("#level-window").window({
                title: "编辑级别",
                iconCls: "icon-tableedit"
            }, "open");
            $("#handle-form").attr("act", "edit");
            $("#handle-form").linkbutton({ text: "保存", iconCls: "icon-tablesave" });
            $("#reset-form").linkbutton({ text: "重载" });
        }
    });

    /** 删除级别 **/
    $("#delete-level").click(function () {
        var selected = $("#level-table").datagrid("getSelected");
        if (selected) {
            $.messager.confirm("删除级别", "您确认删除所选级别信息?<br><span class='required'>*<span>请谨慎删除级别信息!", function (r) {
                if (r) {
                    var args = "", title = "", success_msg = "", error_msg = "", invalid_msg = "";
                    args = "levelID=" + selected.LevelID;
                    title = "删除级别";
                    success_msg = "级别信息删除成功";
                    error_msg = "级别信息删除异常,请重试";
                    invalid_msg = "级别信息无效,请重试";
                    $.ajax({
                        url: "../Level/Delete",
                        type: "POST",
                        data: args,
                        dataType: "json",
                        success: function (data) {
                            if (isSuccess(data)) {
                                gritter(title, success_msg, success);
                                $("#level-table").datagrid("reload");
                            } else if (isError(data)) {
                                gritter(title, error_msg, error);
                            } else if (isInvalid(data)) {
                                gritter(title, invalid_msg, error);
                            }
                        }
                    });
                }
            });
        }
    });

    /** 启用级别  **/
    $("#allow-level").click(function () {
        var disabled = $("#allow-level").linkbutton("options").disabled;
        if (!disabled) {
            enabledLevel("allow");
        }
    });

    /** 禁用级别 **/
    $("#forbid-level").click(function () {
        var disabled = $("#forbid-level").linkbutton("options").disabled;
        if (!disabled) {
            enabledLevel("forbid");
        }
    });

    /** 搜索级别 **/
    $("#search-level").click(function () {
        $("#level-table").datagrid("load", { levelName: $("#slevelname").val() });
    });
});

/***********************
//  启用|禁用级别状态按钮
//  参数:act 动作(启用-"allow"|禁用-"forbid")
************************/
function eabledButtons(act) {
    if (act == "allow") {
        $("#forbid-level").linkbutton("enable");
        $("#allow-level").linkbutton("disable");
    } else if (act == "forbid") {
        $("#forbid-level").linkbutton("disable");
        $("#allow-level").linkbutton("enable");
    }
}

/***********************
//  启用|禁用级别 
//  参数:act 动作[启用-"allow"|禁用-"forbid"]
************************/
function enabledLevel(act) {
    var args = "", title = "",success_msg = "",error_msg = "",invalid_msg = "";
    var selected = $("#level-table").datagrid("getSelected");
    if (selected) {
        if (act == "allow") {
            args = "levelID=" + selected.LevelID + "&flag=1";
            title = "启用级别";
            success_msg = "级别启用成功";
            error_msg = "级别启用异常,请重试";
            invalid_msg = "级别信息无效,请重试!";
        } else if (act == "forbid") {
            args = "levelID=" + selected.LevelID + "&flag=0";
            title = "禁用级别";
            success_msg = "级别禁用成功";
            error_msg = "级别禁用异常,请重试";
            invalid_msg = "级别信息无效,请重试!";
        }
        $.ajax({
            url: "../Level/Enable",
            type: "POST",
            data: args,
            dataType: "json",
            success: function (data) {
                if (isSuccess(data)) {
                    gritter(title, success_msg, success);
                    eabledButtons(act);
                    $("#level-table").datagrid("reload");
                } else if (isError(data)) {
                    gritter(title, error_msg, error);
                } else if (isInvalid(data)) {
                    gritter(title, invalid_msg, error);
                }
            }
        });
    }
}